var prefix = jsctx + "/enterpriseMember";

var enterpriseDiscount = (function () {
    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });

    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
        load();
    };

    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        },10)
    };

    function load() {
        $('#exampleTable')
            .bootstrapTable({
                method: 'get',
                url: prefix + "/listPageEnterpriseDiscount",
                iconSize: 'outline',
                toolbar: '#exampleToolbar',
                striped: false,
                dataType: "json",
                pagination: true,
                singleSelect: false,
                pageSize: 20,
                pageList: [20],
                pageNumber: 1,
                sidePagination: "server",
                cache: false,
                queryParams: function (params) {
                    return {
                        limit: params.limit,
                        offset: (params.offset / params.limit) + 1,
                        enterpriseId: $("#enterpriseId").val().trim()
                    };
                },
                columns: [
                    {
                        checkbox: true, field: '', width: '50', align: 'left',
                    },
                    {
                        field: 'number',
                        title: '序号',
                        width: '100', align: 'center',
                        formatter: function (value, row, index) {
                            return index + 1;
                        }
                    },

                    {field: 'nickname', title: '会员名称', width: '100', align: 'left'},
                    {field: 'mobile', title: '手机号', width: '100', align: 'left'},
                    {field: 'enterpriseName', title: '企业名称', width: '100', align: 'left'},
                    {field: 'stationName', title: '桩群名称', width: '100', align: 'left'},
                    {
                        field: 'discountType',
                        title: '折扣类型',
                        width: '100', align: 'left',
                        formatter: function (value, row, index) {
                            if (row.discountType == '0') {
                                return '<span >服务费折扣</span>';
                            } else if (row.discountType == '2') {
                                return '<span >总价折扣</span>';
                            }
                        }
                    },
                    {
                        field: '', title: '服务费折扣', width: '100', align: 'right',
                        formatter: function (value, row, index) {
                            if (row.discountType == '0') {
                                var z = '';
                                for (var i = 0; i < row.discountTimeSaveVoList.length; i++) {
                                    var a = row.discountTimeSaveVoList[i].discount;
                                    z += a + '<br>';
                                }
                                return z;
                            } else {
                                return '-'
                            }
                        }
                    },
                    {
                        field: '', title: '总价折扣', width: '100', align: 'right',
                        formatter: function (value, row, index) {
                            if (row.discountType == '2') {
                                var z = '';
                                for (var i = 0; i < row.discountTimeSaveVoList.length; i++) {
                                    var a = row.discountTimeSaveVoList[i].discount;
                                    z += a + '<br>';
                                }
                                return z;
                            } else {
                                return '-'
                            }
                        }
                    },
                    {
                        field: '', title: '时段', width: '200', align: 'left',
                        formatter: function (value, row, index) {
                            var z = '';
                            for (var i = 0; i < row.discountTimeSaveVoList.length; i++) {
                                var a = row.discountTimeSaveVoList[i].startPeriod;
                                var b = '~';
                                var c = row.discountTimeSaveVoList[i].endPeriod;
                                z += a + ' ' + b + ' ' + c + '<br>';

                            }
                            return z;
                        }
                    },
                    {field: 'policyDesc', title: '备注', width: '100', align: 'left'},
                    {
                        title: '操作',
                        field: 'orderId',
                        width: '200', align: 'center',
                        formatter: function (value, row, index) {
                            var a = '<a class="btn btn-success btn-sm ' + has_edit + '" href="#" mce_href="#" title="子会员折扣" onclick="enterpriseDiscount.addEnterpriseSubmember(\''
                                + row.discountStationId + '\')">子会员折扣</a> ';

                            var b = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#" mce_href="#" title="编辑" onclick="enterpriseDiscount.editEnterpriseDiscount(\''
                                + row.discountStationId + '\')">编辑</a> ';

                            var c = '<a class="btn btn-danger btn-sm ' + has_edit + '" href="#" mce_href="#" title="删除" onclick="enterpriseDiscount.deleteEnterpriseDiscountStation(\''
                                + row.discountStationId + '\')">删除</a> ';

                            return b + c;
                        }
                    }
                ]
            });
    };

    function editEnterpriseDiscount(discountStationId) {
        location.href = prefix + "/editEnterpriseDiscount/" + discountStationId;
    }

    function addEnterpriseDiscount(enterpriseId) {
        location.href = prefix + "/addEnterpriseDiscount/" + enterpriseId;
    }

    function deleteEnterpriseDiscountStation(discountStationId) {
        layer.confirm('确定要删除选中的记录？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/deleteEnterpriseDiscountStation",
                type: "post",
                data: {
                    'discountStationId': discountStationId
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("删除成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }

    //批量删除站点
    function selectDiscountStation() {
        var rows = $("#exampleTable").bootstrapTable('getSelections');// 获得要删除的数据
        if (rows.length == 0) {// rows 主要是为了判断是否选中，下面的else内容才是主要
            alert("请先选择站点!");
            return;
        } else {
            var discountStationList = new Array();// 声明一个数组
            $(rows).each(function () {// 通过获得别选中的来进行遍历
                discountStationList.push(this.discountStationId); // cid为获得到的整条数据中的一列
            });
            deleteBatchEnterpriseDiscountStation(discountStationList)
        }
    }

    function deleteBatchEnterpriseDiscountStation(discountStationList) {
        layer.confirm('确定要删除选中站点？', {
            title: '删除站点折扣', content: '批量删除站点折扣', btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/deleteBatchEnterpriseDiscountStation",
                type: "post",
                traditional: true,
                data: {
                    discountStationList: discountStationList,
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("删除成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }


    return {
        init: init,
        reLoad: reLoad,
        editEnterpriseDiscount: editEnterpriseDiscount,
        deleteEnterpriseDiscountStation: deleteEnterpriseDiscountStation,
        addEnterpriseDiscount: addEnterpriseDiscount,
        selectDiscountStation: selectDiscountStation
    };
})();
